package nl.hva.web.mvcwebapp.doa;

import java.util.ArrayList;
import java.util.List;
import nl.hva.web.mvcwebapp.models.Chapter;
import nl.hva.web.mvcwebapp.models.Page;
import nl.hva.web.mvcwebapp.models.PageWidget;
import nl.hva.web.mvcwebapp.models.Widget;
import nl.hva.web.mvcwebapp.service.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class PageDAO {

    private static Page page;

    public PageDAO() {
    }
    /*
     * Page Related Methods
     */

    public static List<Page> getAllPages() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Page> page_list = session.createCriteria(Page.class).list();
        tx.commit();
        return page_list;
    }

    public static Page getPage(int page_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Page page = (Page) session.get(Page.class, page_id);
        tx.commit();
        return page;
    }

    public static Page getPageByPath(String page_path) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Page page = (Page) session.get(Page.class, page_path);
        tx.commit();
        return page;
    }

    public static ArrayList insertPage(Page page) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        try {
            session.save(page);
            tx.commit();
        } catch (Exception e) {
        }
        return validation_errors;
    }

    public static ArrayList updatePage(Page page) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(page);
        tx.commit();
        return validation_errors;
    }

    public static void deletePage(int page_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Page page = (Page) session.load(Page.class, page_id);
        session.delete(page);
        tx.commit();
    }

    /*
     * Page_Widget Related Methods
     */
    public static List<PageWidget> getAllPageWidgets() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<PageWidget> pagewidget_list = session.createCriteria(PageWidget.class).list();
        tx.commit();
        return pagewidget_list;
    }

    public static List<Widget> getWidgetsByPath(String path) {
        List<Widget> widget_list = null;


        List<PageWidget> pagewidget_list = null;

        AnnotationConfiguration config = new AnnotationConfiguration();
        config.addAnnotatedClass(Chapter.class);
        SessionFactory factory = config.configure().buildSessionFactory();
        Session session = factory.getCurrentSession();
        session.beginTransaction();
        //TODO Fix Query
        Query queryResult = session.createQuery("FROM Chapter WHERE course_courseid = '" + path + "'");

        pagewidget_list = queryResult.list();



        for (int i = 0; i < pagewidget_list.size(); i++) {
            widget_list.add(pagewidget_list.get(i).getWidget());
        }

        return widget_list;
    }

    public static ArrayList insertPageWidget(PageWidget pagewidget) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(pagewidget);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList updatePageWidget(PageWidget pagewidget) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(pagewidget);
        tx.commit();
        return validation_errors;
    }

    public static void deletePageWidget(int pagewidget) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Page page = (Page) session.load(Widget.class, pagewidget);
        session.delete(page);
        tx.commit();
    }

    /*
     * Widget Related Methods
     */
    public static List<Widget> getAllWidgets() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        List<Widget> widget_list = session.createCriteria(Widget.class).list();
        tx.commit();
        System.out.println("GetAll");
        for (int i = 0; i < widget_list.size(); i++) {
            System.out.println(widget_list.get(i).getWidgetId());
        }
        return widget_list;
    }

    public static Widget getWidget(int widget_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Widget widget = (Widget) session.load(Widget.class, widget_id);
        tx.commit();
        return widget;
    }

    public static ArrayList insertWidget(Widget widget) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.save(widget);
        tx.commit();
        return validation_errors;
    }

    public static ArrayList updateWidget(Widget widget) {
        ArrayList validation_errors = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        session.update(widget);
        tx.commit();
        return validation_errors;
    }

    public static void deleteWidget(int widget_id) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tx = session.beginTransaction();
        Page page = (Page) session.load(Widget.class, widget_id);
        session.delete(page);
        tx.commit();
    }
}
